<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i>
                    {{pageTitle}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container" :style="{'min-height': containerHeight + 'px'}">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-folder-add" @click="goAdd">新增</el-button>
                <el-button type="warning" icon="el-icon-edit" @click="goEdit">修改</el-button>
            </div>

            <!-- 搜索表单 -->
            <el-form :model="query" ref="searchRuleForm" label-width="100px">
                <el-row>
                    <el-col :sm="20" :md="6">
                        <el-form-item label="用户名:" prop="account">
                            <el-input v-model="query.account" placeholder="请输入" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :sm="20" :md="6">
                        <el-form-item label="姓名:" prop="name">
                            <div @click="showTreeDialog('姓名')">
                                <el-input v-model="query.name" placeholder="请输入">
                                    <i slot="suffix" class="el-icon-search"></i>
                                </el-input>
                            </div>
                        </el-form-item>
                    </el-col>

                    <el-col :sm="20" :md="6" align="center">
                        <el-button
                            type="primary"
                            icon="el-icon-search"
                            @click="submitForm('searchRuleForm')"
                        >搜索</el-button>
                        <el-button type="info" @click="resetForm('searchRuleForm')">重置</el-button>
                    </el-col>
                </el-row>
            </el-form>

            <div class="tree-box">
                <div class="table-box">
                    <table-List
                        ref="tableList"
                        :tableData="tableData"
                        :tableHeader="tableHeader"
                        :loading="loading"
                        :pagination="pagination"
                        @pageChange="pageChange"
                        @selectFun="selectFun"
                        @sortChange="sortChange"
                    ></table-List>
                </div>
            </div>
        </div>
        <!-- 人员搜索 -->
        <tree-search
            :isShow="showSearch"
            :dialogTitle="searchDialogTit"
            @edit="searchBack"
            @close="closeTreeDialog"
        ></tree-search>
    </div>
</template>


<script>
import appMain from '../../../utils/app_main';
export default {
    name: 'welfareList',
    data() {
        return {
            pageTitle: '',
            containerHeight: '',
            query: {
                account: '', //账号
                departId:'',//部门id
                name: '', //部门ID
                sortColumn: '' //排序字段 倒序追加 columnName desc
            },
            searchDialogTit: '',
            showSearch: false,
            // 通用表格
            loading: false,
            pagination: {
                pageIndex: 1,
                pageSize: 10,
                pageTotal: 0
            },
            tableData: [
                // {
                //     account: 'string', //用户名-账号
                //     departIsCompany: 'string', //
                //     departNo: 'string', //
                //     departParentId: 'string', //
                //     description: 'string', //
                //     hierarchy: 'string', //
                //     higherUpAccount: 'string', //上级领导账号
                //     higherUpId: 'string', //上级领导ID
                //     higherUpName: 'string', //上级领导名字
                //     isLock: 0, //是否锁定，1 否  2锁定
                //     jobId: 'string', //岗位ID
                //     jobName: 'string', //岗位名字
                //     name: 'string', //姓名
                //     organizationId: 'string', //组织ID
                //     organizationName: 'string', //组织名
                //     password: 'string', //
                //     remark: 'string', //
                //     responsibility: 'string', //
                //     roles: [], //角色id
                //     signature: 'string' //签名照片
                // }
            ],
            tableHeader: [
                // 表头数据
                { prop: 'account', label: '账户名', minWidth: '100px', router: 'useraccountDetail', routerId: 'id', sortable: 'custom' },
                {
                    prop: 'name',
                    label: '姓名',
                    sortable: 'custom'
                },
                {
                    prop: 'isLock',
                    label: '是否锁定',
                    formatData: function(val) {
                        if (val == 1) {
                            return '否';
                        } else if (val == 2) {
                            return '锁定';
                        } else {
                            return '未设置';
                        }
                    },
                    sortable: 'custom'
                },

                { prop: 'organizationName', label: '部门', minWidth: '100px', sortable: 'custom' },
                { prop: 'jobName', label: '岗位', minWidth: '100px', sortable: 'custom' }
            ],
            multipleSelection: []
        };
    },
    mounted() {
        var div_height = window.screen.availHeight;
        var height = div_height - 100;
        this.containerHeight = height - 160;
        this.pageTitle = this.$router.currentRoute.meta.title;
        // console.log(this.$router, 'router')
    },
    created() {
        this.getData();
    },
    methods: {
        getData() {
            this.loading = true;
            console.log('getData', this.loading);
            let params = {
                pageNo: this.pagination.pageIndex,
                pageSize: this.pagination.pageSize,
                record: this.query
            };

            appMain
                .accountFindPageList(params)
                .then(res => {
                    if (res.data.code === 20000) {
                        //TODO 测试环境使用
                        this.timer = setTimeout(() => {
                            //设置延迟执行
                            this.loading = false;
                        }, 1000);

                        this.tableData = res.data.data.list;
                        this.pagination.pageSize = res.data.data.pageSize;
                        this.pagination.pageIndex = res.data.data.pageNum;
                        this.pagination.pageTotal = res.data.data.total;

                        console.log('getData tableData', this.tableData);
                    } else {
                        this.loading = false;
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    this.loading = false;
                    console.log(err);
                });
        },
        goEdit() {
            if (this.multipleSelection.length < 1) {
                this.$message('请选择一条记录');
                return;
            }
            if (this.multipleSelection.length > 1) {
                this.$message('只能选择一条记录');
                return;
            }
            this.$router.push({
                path: '/useraccount',
                query: {
                    id: this.multipleSelection[0].id
                }
            });
        },
        goAdd() {
            this.$router.push({
                path: '/useraccount'
            });
        },
        resetForm(formName) {
            console.log('resetForm');
            this.query = {
                account: '', //账号
                departId:'',//部门id
                name: '', //部门ID
                sortColumn: '' //排序字段 倒序追加 columnName desc
            };
            this.$refs.tableList.clearSort();
            this.$refs[formName].resetFields();
        },
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.pagination = {
                        pageIndex: 1,
                        pageSize: 10,
                        pageTotal: 0
                    };
                    this.getData();
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },

        // 选择姓名
        showTreeDialog(tit) {
            console.log('showTreeDialog');
            this.searchDialogTit = tit;
            this.showSearch = true;
        },
        // 树组件搜索结果返回
        searchBack(data) {
            console.log(data.backData, 'searchTreeBack');
            this.query.name = data.backData.name;
            this.query.departId = data.backData.organizationId;
            this.showSearch = false;
        },
        closeTreeDialog(data) {
            this.showSearch = data.showDialog;
        },
        //表格
        // 分页导航
        pageChange(data) {
            this.pagination = data.backData;
            this.getData();
        },
        // 组件选择完后把数据传过来
        selectFun(data) {
            this.multipleSelection = data.backData;
        },
        //表格组件返回排序对象
        sortChange(data) {
            let column = data.backData;
            //排序
            if (column.order) {
                //倒序
                if (column.order === 'descending') {
                    this.query.sortColumn = column.prop + ' ' + 'desc';
                } else {
                    this.query.sortColumn = column.prop;
                }
            } else {
                //不排序
                this.query.sortColumn = '';
            }
            this.getData();
        }
    }
};
</script>


<style lang="css" scoped>
.container {
    box-sizing: border-box;
}
.handle-box {
    margin-bottom: 20px;
    flex-wrap: wrap;
    font-size: 12px;
}
.handle-box > div,
.handle-box > button {
    margin-bottom: 15px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 120px;
    display: inline-block;
}
.tree-box {
    border: 1px solid #ddd;
    margin-top: 20px;
    height: calc(100% - 146px);
}
.table-box {
    height: calc(100% - 72px);
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.ml10 {
    margin-left: 10px;
}
</style>